﻿using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Linq;
using System.Text;
using System.Windows.Forms;

namespace HelperPrototype.OtherForms
{
    public partial class ThemPhieuNhapKho : Form
    {
        private string _maPhieu;

        public ThemPhieuNhapKho()
        {
            InitializeComponent();
        }

        public ThemPhieuNhapKho(string maPhieuNhapKho)
        {
            InitializeComponent();
            _maPhieu = maPhieuNhapKho;
        }

        private void ThemPhieuNhapKho_Load(object sender, EventArgs e)
        {
            loadData();
        }

        private void loadData()
        {
            _DB.DataClasses1DataContext dc = new _DB.DataClasses1DataContext();

            // if edit, show info
            if (_maPhieu != null)
            {
                _DB.PhieuNhapKho p = dc.PhieuNhapKhos.Single(x => x.MaPhieu == _maPhieu);
                txtMaPhieu.Text = p.MaPhieu;
                txtMaPhieu.ReadOnly = true;
                txtNoiDung.Text = p.NoiDung;
                cbbDonDatHang.Items.Add(p.DonDatHang);
                cbbDonDatHang.SelectedItem = p.DonDatHang;
                cbbDonDatHang.Enabled = false;
            }
            // new
            else
            {
                txtMaPhieu.Text = _DB.DinhDangMaPhieu.getMaPhieuMoi(_DB.DinhDangMaPhieu.DINH_DANG_PHIEU_NHAP_KHO_ID);
                
                // fill combobox don dat hang
                var listDonDH   = from d in dc.DonDatHangs
                                  let qt = d.QuyTrinhMuaHang
                                  where qt.deleted == false &&
                                        d.PhieuNhapKhos.Count(x => x.deleted == false) == 0 &&
                                        (qt.TrangThai == _DB.TrangThaiQuyTrinhMuaHang.DANG_MUA_HANG ||
                                         qt.TrangThai == _DB.TrangThaiQuyTrinhMuaHang.DA_DUYET_PHIEU_TAM_UNG ||
                                         qt.TrangThai == _DB.TrangThaiQuyTrinhMuaHang.DANG_NHAP_KHO)
                                  select d;
                cbbDonDatHang.DataSource = listDonDH;
            }
        }

        private void button3_Click(object sender, EventArgs e)
        {
            XemDonDatHang f = new XemDonDatHang(cbbDonDatHang.Text);
            f.Show();
        }

        private void comboBox1_SelectedIndexChanged(object sender, EventArgs e)
        {
            // check
            _DB.DonDatHang d = (_DB.DonDatHang)cbbDonDatHang.SelectedItem;
            if (d == null)
                return;

            // fill grid hang hoa
            int count = 1;
            dgvHangHoa.Rows.Clear();
            foreach (_DB.HangHoaDonDatHang h in d.HangHoaDonDatHangs)
            {
                _DB.PhieuDeNghiMuaHang_HangHoa hh = h.PhieuDeNghiMuaHang_HangHoa;
                
                DataGridViewRow r = new DataGridViewRow();
                r.CreateCells(dgvHangHoa);
                r.Cells[0].Value = count++;
                r.Cells[1].Value = hh.HangHoa.TenHangHoa;
                if (hh.SoLuongNhapKho != null)
                    r.Cells[2].Value = hh.SoLuongNhapKho.ToString();
                else
                    r.Cells[2].Value = hh.SoLuongMua.ToString();
                r.Cells[3].Value = hh.HangHoa.DonVi;
                r.Tag = h.ID;

                dgvHangHoa.Rows.Add(r);
            }
        }

        private void button1_Click(object sender, EventArgs e)
        {
            this.Close();
        }

        private void button2_Click(object sender, EventArgs e)
        {
            try
            {
                // check
                _DB.DonDatHang d = (_DB.DonDatHang)cbbDonDatHang.SelectedItem;
                if (d == null)
                    return;

                // go for phieu nhap kho
                _DB.DataClasses1DataContext dc = new _DB.DataClasses1DataContext();
                _DB.PhieuNhapKho p;
                if (_maPhieu == null)
                {
                    // new
                    p = new _DB.PhieuNhapKho();
                    p.MaPhieu = txtMaPhieu.Text;
                    p.MaDonDatHang = d.MaDonDatHang;
                    p.MaNguoiTao = Program.MaNguoiDungHienTai;
                    p.NgayTao = DateTime.Now;
                    p.NoiDung = txtNoiDung.Text;
                    dc.PhieuNhapKhos.InsertOnSubmit(p);
                }
                else
                {
                    p = dc.PhieuNhapKhos.Single(x => x.MaPhieu == _maPhieu);
                    p.NoiDung = txtNoiDung.Text;
                }

                dc.SubmitChanges();

                // update so luong nhap kho
                foreach (_DB.HangHoaDonDatHang h in p.DonDatHang.HangHoaDonDatHangs)
                foreach (DataGridViewRow r in dgvHangHoa.Rows)
                {
                    if (h.ID == (int) r.Tag)
                    {
                        h.PhieuDeNghiMuaHang_HangHoa.SoLuongNhapKho = int.Parse(r.Cells[2].Value as string);
                    }
                }

                // update quy trinh
                _DB.QuyTrinhMuaHang qt = p.DonDatHang.QuyTrinhMuaHang;
                bool daNhapHet = true;
                foreach (_DB.DonDatHang ddh in qt.DonDatHangs)
                {
                    if (dc.PhieuNhapKhos.Count(x => x.MaDonDatHang == ddh.MaDonDatHang && x.deleted == false) == 0)
                        daNhapHet = false;
                }
                if (daNhapHet)
                    qt.TrangThai = _DB.TrangThaiQuyTrinhMuaHang.DA_NHAP_KHO;
                else
                    qt.TrangThai = _DB.TrangThaiQuyTrinhMuaHang.DANG_NHAP_KHO;

                dc.SubmitChanges();
                this.Close();
            }
            catch (System.Exception ex)
            {
                MessageBox.Show(ex.Message, "Error");
            }
        }
    }
}
